import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
import numpy as np
from sklearn.cluster import AgglomerativeClustering
import scipy.cluster.hierarchy as sch

plt.figure(figsize=(20, 4))

n_samples = 1500
x,y = make_blobs(n_samples=n_samples, random_state=170)

#points = scipy.randn(20,4)
plt.subplot(141)
plt.scatter(x[:,0],x[:,1], c=y)
plt.title("original")


import scipy.cluster.hierarchy as sch
dis_mat = sch.distance.pdist(x,'euclidean')
z = sch.linkage(dis_mat, method='average')
#sch.dendrogram(z)
#plt.savefig('ceng.png')

cluster = sch.fcluster(z,3, criterion='maxclust')
plt.subplot(142)
plt.scatter(x[:,0],x[:,1], c=cluster)
plt.title("Hierarchy")

from sklearn import metrics
from sklearn.cluster import DBSCAN

db = DBSCAN(eps=0.5, min_samples=10).fit(x)
labels = db.labels_
plt.subplot(143)
plt.scatter(x[:,0],x[:,1], c=labels)
plt.title("DBSCAN")

centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(n_samples=300, 
                            centers=centers, 
                            cluster_std=0.5,
                            random_state=0)

from sklearn.cluster import AffinityPropagation
af = AffinityPropagation(preference=-50).fit(X)
af_label = af.labels_
print(af_label)

plt.subplot(144)
plt.scatter(X[:,0],X[:,1], c=af_label)
plt.title("AF")


plt.savefig('cluser2.png')

